package com.ouman.luoliluoli.adapters;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.v7.widget.CardView;
import android.support.v7.widget.PopupMenu;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.ouman.luoliluoli.R;
import com.ouman.luoliluoli.models.DaysModel;
import com.ouman.luoliluoli.utils.RandomUIChooser;
import com.ouman.luoliluoli.utils.LocalSaveHelper;

import java.util.Collections;
import java.util.List;
import java.util.Random;

/**
 * Created by jintian on 05/04/2017.
 */

public class DaysRecyclerViewAdapter extends RecyclerView.Adapter<DaysRecyclerViewAdapter.DaysViewHolder>{

    private Context context;
    private List<DaysModel> data;
    private LocalSaveHelper saveHelper;

    private int[] mCardHeight = {2, 4};

    public DaysRecyclerViewAdapter(Context context, List<DaysModel> data){
        this.context = context;
        this.data = data;

    }

    public DaysRecyclerViewAdapter.DaysViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
        View view = LayoutInflater.from(context).inflate(R.layout.recyclerview_item_days, null);

        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                System.out.println("===>>click!!!!!");
            }
        });

        return new DaysRecyclerViewAdapter.DaysViewHolder(view);
    }

    public void onBindViewHolder(final DaysRecyclerViewAdapter.DaysViewHolder holder, final int position){
        String title = data.get(position).getTitle();
        String desc = data.get(position).getDesc();
        Integer daysDelta = data.get(position).getDaysDelta();

        //TODO: 拿到时间，判断和当前时间的先后，如果已经过了就把文字标识为过期

        holder.imageViewDaysBk.setImageResource(context.getResources().getIdentifier(RandomUIChooser.materialBk[new Random().nextInt(7)], "drawable", context.getPackageName()));
        holder.textViewDaysDelta.setTextColor(Color.parseColor("#"+ RandomUIChooser.deepColors[new Random().nextInt(18)]));

        holder.textViewTitle.setText(title);
        if (data.get(position).getDone() || daysDelta < 0){
            holder.textViewTitle.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
            holder.textViewDaysDelta.setText("PAS.");
        } else {
            holder.textViewTitle.getPaint().setFlags(0);
            System.out.println("get days delta set to recycler: " + daysDelta);
            holder.textViewDaysDelta.setText(daysDelta.toString());
        }

        holder.textViewDesc.setText(desc);
//        holder.cardViewDays.setCardBackgroundColor(Color.parseColor("#"+ RandomUIChooser.mColors[new Random().nextInt(254)]));
        holder.cardViewDays.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });

        Animation animation = AnimationUtils.loadAnimation(context, R.anim.anim_card_show);
        holder.cardViewDays.startAnimation(animation);

        holder.dropdown.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final PopupMenu popupMenu = new PopupMenu(context, holder.dropdown);
                popupMenu.getMenuInflater().inflate(R.menu.days_popup_memu, popupMenu.getMenu());
                popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                    @Override
                    public boolean onMenuItemClick(MenuItem item) {
                        int id = item.getItemId();
                        saveHelper = new LocalSaveHelper();
                        if (id == R.id.daysPopupMenuRemove){
                            data.remove(position);
                            notifyItemRemoved(position);
                            notifyDataSetChanged();
                            saveHelper.removeDaysDataArrayItem(context, position);
                            Toast.makeText(context,"移除成功", Toast.LENGTH_SHORT).show();
                        } else if (id == R.id.daysPopupMenuDone){
                            data.get(position).setDone(true);
                            saveHelper.updateDaysDataArrayItem(context, position, data.get(position));
                            notifyDataSetChanged();
                            Toast.makeText(context,"已标记为完成，将会在7天后自动移除", Toast.LENGTH_SHORT).show();
                        } else if (id == R.id.daysPopupMenuPrior){
                            data.get(position).setImportanceLevel("0");
                            saveHelper.updateDaysDataArrayItem(context, position, data.get(position));
                            notifyDataSetChanged();
                            Toast.makeText(context,"已标记为优先级最高，置顶显示", Toast.LENGTH_SHORT).show();
                        }
                        return true;
                    }
                });
                popupMenu.show();
            }
        });

    }


    public int getItemCount(){
        return data.size();
    }

    public List getDataList(){
        return data;
    }

    public void onItemDismiss(int position) {
        //移除数据
        data.remove(position);
        notifyItemRemoved(position);
    }

    public class DaysViewHolder extends RecyclerView.ViewHolder {

        TextView textViewTitle;
        TextView textViewDesc;
        TextView textViewDaysDelta;
        CardView cardViewDays;
        ImageView imageViewDaysBk;
        Button dropdown;
        public DaysViewHolder(View view){
            super(view);
            imageViewDaysBk = (ImageView) view.findViewById(R.id.imageViewDaysBk);
            textViewTitle = (TextView) view.findViewById(R.id.textview_days_title);
            textViewDesc = (TextView) view.findViewById(R.id.textview_days_desc);
            textViewDaysDelta = (TextView) view.findViewById(R.id.textview_days_left);
            cardViewDays = (CardView) view.findViewById(R.id.day_cardview);
            dropdown = (Button) view.findViewById(R.id.buttonDropDownDays);

        }

    }

}


